<template>
  <div style="positon:relative;">
    <div
      v-show="showSeason"
      style="position:fixed;top:0;bottom:0;left:0;right:0;background:rgba(0,0,0,0);z-index:999;"
      @click.stop="showSeason=false"
    />
    <el-input
      v-model="showValue"
      clearable
      placeholder="请选择季度"
      :style="inputStyle"
      v-bind="$attrs"
      @focus="showSeason=true"
      @clear="clear"
      v-on="$listeners"
    >
      <i slot="prefix" class="el-input__icon el-icon-date" />
    </el-input>
    <el-card
      v-show="showSeason"
      class="box-card"
      style="width:322px;padding: 0 3px 20px;margin-top:10px;position:absolute;z-index:9999"
    >
      <div slot="header" class="clearfix" style="text-align:center;padding:0">
        <button
          type="button"
          aria-label="前一年"
          class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left"
          @click="prev"
        />
        <span role="button" class="el-date-picker__header-label">{{ year }}年</span>
        <button
          type="button"
          aria-label="后一年"
          class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right"
          @click="next"
        />
      </div>
      <div class="text item" style="text-align:center;">
        <el-button
          type="text"
          size="medium"
          style="width:40%;color: #606266;float:left;"
          @click.stop="selectSeason(0)"
        >
          第一季度
        </el-button>
        <el-button
          type="text"
          size="medium"
          style="float:right;width:40%;color: #606266;"
          @click.stop="selectSeason(1)"
        >
          第二季度
        </el-button>
      </div>
      <div class="text item" style="text-align:center;">
        <el-button
          type="text"
          size="medium"
          style="width:40%;color: #606266;float:left;"
          @click.stop="selectSeason(2)"
        >
          第三季度
        </el-button>
        <el-button
          type="text"
          size="medium"
          style="float:right;width:40%;color: #606266;"
          @click.stop="selectSeason(3)"
        >
          第四季度
        </el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
/**
 * @file:  View 组件 季节选择控件
 * @author: v_zhuchun
 * @date: 2019-05-23
 * @description: UI组件  可选择季节
 * @api: valueArr : 季度value defalut['01-03', '04-06', '07-09', '10-12'] 默认值待设置
 */
export default {
  model: {
    prop: 'value',
    event: 'season'
  },
  props: {
    // eslint-disable-next-line vue/require-default-prop
    inputStyle: Object,
    valueArr: {
      default: () => {
        return ['01-03',
          '04-06',
          '07-09',
          '10-12']
      },
      type: Array
    },
    getValue: {
      default: () => {},
      type: Function
    },
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      showSeason: false,
      season: '',
      year: new Date().getFullYear()
      // showValue: ''
    }
  },
  computed: {
    showValue () {
      const value = this.value
      if (value.length < 1) return ''
      const arr = value.split(',')
      return `${arr[0]}年${arr[1]}季度`
    }
  },
  methods: {
    one () {
      this.showSeason = false
    },
    prev () {
      this.year = this.year * 1 - 1
    },
    next () {
      this.year = this.year * 1 + 1
    },
    selectSeason (i) {
      const that = this
      that.season = i + 1
      const arr = that.valueArr[i].split('-')
      that.getValue(that.year + arr[0] + '-' + that.year + arr[1])
      this.showSeason = false
      // this.showValue = `${this.year}年${this.season}季度`
      this.$emit('season', `${this.year},${this.season}`)
    },
    clear () {
      this.$emit('season', '')
    },
    focus () {
      this.showSeason = true
    }
  }
}
</script>
